package com.supply.dl.activity.seachmode;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.supply.dl.R;
import com.supply.dl.activity.BaseActivity;
import com.supply.dl.activity.FeedSheetListActivity;
import com.supply.dl.activity.SearchActivity;
import com.supply.dl.app.ApiService;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.adapter.NormalAdapter;
import com.supply.dl.app.adapter.ViewHolder;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.app.http.BaseObserver;
import com.supply.dl.app.http.HttpMethod;
import com.supply.dl.app.http.RxSchedulers;
import com.supply.dl.bean.TouliaoOrderInfo;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.DateUtils;
import com.supply.dl.utils.StringUtils;

import android.support.v4.util.ArrayMap;

import java.util.ArrayList;
import java.util.List;


/**
 * 投料作业订单列表
 * <p>
 * Author: tian <br>
 * Time: 2018/6/12 16:07
 */
public class FeedOperationOrderModle extends ISearchModle<TouliaoOrderInfo> {

    @Override
    public void setSearchAdapter() {
        if (adapter != null)
            return;

        resource = new ArrayList<>();

        adapter = new NormalAdapter<TouliaoOrderInfo>(context, resource) {
            @Override
            protected void convert(ViewHolder holder, TouliaoOrderInfo data) {
                //订单类型名字
                holder.setText(R.id.name_tv, data.getOrderTypeName());
                //状态
                holder.setText(R.id.status_tv, data.getOrderStatusName());
                //生产订单号
                holder.setText(R.id.order_nub_tv, "生产订单号:" + data.getErpOrderNo());
                //生产订单号
                holder.setText(R.id.erp_nub_tv, "生产车间:" + data.getProductWorkshopName());
                //添加时间
                holder.setText(R.id.time_tv, StringUtils.isEmpty(String.valueOf(data.getAddTime())) ? ""
                        : DateUtils.getSimpleDateTimeByMill(String.valueOf(data.getAddTime()), DateUtils.DATE_TIME_FORMAT));
            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.item_obtainmaterial;
            }
        };

        adapter.setOnItemClickListener(new NormalAdapter.OnItemClickListener<TouliaoOrderInfo>() {
            @Override
            public void onItemClick(ViewHolder viewHolder, TouliaoOrderInfo data, int position) {
                BaseActivity ac = (BaseActivity) context;
                Bundle bundle = new Bundle();
                bundle.putString("orderId", data.getId());
                ac.jumpActivity(FeedSheetListActivity.class, bundle);
            }
        });

    }

    @Override
    public void setHintText() {
        hintText = "领料单号，生产订单号";
    }

    @Override
    public void setTitleShow() {
        titleShow = "投料订单列表";
    }

    @Override
    public void init(Context context) {
        this.context = context;

        setTitleShow();
        setSearchAdapter();
    }


    @Override
    public void vistData(String keyWord, ArrayMap<String, String> filterMap, OnVistDataSuccess onVistDataSuccess) {
        String companyId = filterMap.get(AppConfig.SearchFilterAction.COMPANY);
        String factoryId = filterMap.get(AppConfig.SearchFilterAction.FACTORY);
        String productWorkshopId = filterMap.get(AppConfig.SearchFilterAction.WORKSHOP);

        ApiService api = HttpMethod.getInstance().create(ApiService.class);
        api.getTouliaoOrderList(keyWord, companyId, factoryId, productWorkshopId,
                String.valueOf(pageNum), HttpManger.PAGE_SIZE)
                .compose(RxSchedulers.<BaseInfo<List<TouliaoOrderInfo>>>compose(context))
                .subscribe(new BaseObserver<List<TouliaoOrderInfo>>() {
                    @Override
                    public void onSuccess(BaseInfo<List<TouliaoOrderInfo>> baseInfo) {

                        if (baseInfo != null && baseInfo.getData() != null) {
                            if(pageNum == 1){
                                resource.clear();
                            }
                            if(baseInfo.getData().size() < 10){
                                hasMoreData = false;
                            }
                            resource.addAll(baseInfo.getData());
                            adapter.notifyDataSetChanged();
                        }
                        if(resultListener != null)
                            resultListener.onResult();
                    }

                    @Override
                    public void onFailure(String msg) {
                        if(resultListener != null)
                            resultListener.onResult();
                    }
                });

//        resource.clear();
//
//        ArrayList<String> returnData = new ArrayList<String>();
//        resource.addAll(returnData);
//        adapter.notifyDataSetChanged();
//
//        if (onVistDataSuccess != null)
//            onVistDataSuccess.showUi(resource.size() == 0);
    }

}
